package com.starblink.store.recommend.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.bean.LoadAction
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.FollowStoreEvent
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.store.databinding.FragmentRecommendStoreBinding
import com.starblink.store.databinding.ItemRecommendStoreBinding
import com.starblink.store.recommend.adapter.RecommendStoreHolder

class StoreRecommendFragment() :
    BaseTVMFragment<FragmentRecommendStoreBinding, RecommendStoreVM>() {
    val vm: RecommendStoreVM = TODO()
    val title: String = ""
    companion object {
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentRecommendStoreBinding {
        return FragmentRecommendStoreBinding.inflate(inflater, container, false)
    }

    private val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(vm.recommendList.value!!).apply {
            addVhDelegate {
                RecommendStoreHolder(
                    ItemRecommendStoreBinding.inflate(it.inflater, it, false),
                    mActivity, 1, context.hashCode().toString()
                )
            }
        }
    }

    /**
     * 初始化数据
     */
    override fun initData() {
        if (title.isNotEmpty()) {
            viewBinding.tvPopular.text = title
        }
        val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        viewBinding.rv.layoutManager = linearLayoutManager

        viewBinding.rv.adapter = adapter//单位px

        //同步订阅数据
        FlowBus.with<FollowStoreEvent>(FlowConst.FOLLOW_STORE_EVENT).register(this) {
            for ((index, merchat) in viewModel.myList.value!!.withIndex()) {
                if (merchat != null && merchat is MerchantWebVOF && it.id == merchat.id
                    && it.followed != merchat.subOrNot
                ) {
                    ApolloValSetExt.setApolloVal<MerchantWebVOF>(merchat, "subOrNot", it.followed)
                    viewModel.recommendList.value!![index] = merchat
                    viewModel.recommendList.value = viewModel.myList.value
                }
            }
        }
    }

    override fun startObserve() {
        vm.recommendList.observe(this) {
            if (vm.recommendList.value!!.isNotEmpty()) {
//                adapter.notifyDataSetChanged()
                adapter.notifyItemRangeChanged(vm.recPositionStart, vm.recItemCount)
                viewBinding.ivLoading.visibleOrGone(false) {
                }

//                val layoutParams = viewBinding.rv.layoutParams
//                layoutParams.height = dp2px(164f) * it.size
//                viewBinding.rv.layoutParams = layoutParams
            }
        }

        vm.mLoadActionLiveData.observe(this) {
            if (vm.mLoadActionLiveData.value == LoadAction(LoadAction.STATE_NO_DATA)) {
                viewBinding.empty.visibleOrGone(true) {
//                    viewBinding.refresher.visibility = View.GONE
                }
                viewBinding.ivLoading.visibleOrGone(false) {
                }
            }
        }
    }

}